package com.happyclub.lcy.service.impl;

import com.happyclub.lcy.dao.*;
import com.happyclub.lcy.service.ITeamService;
import com.happyclub.pojo.*;
import com.happyclub.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

import static com.happyclub.utils.SDF.getDate;
import static com.happyclub.utils.Uuid.getUUID;
@Service
public class TeamServiceImpl implements ITeamService {

    @Autowired
    private ITeam iTeam;
    @Autowired
    private IUserTeam iUserTeam;
    @Autowired
    private IUserRole iUserRole;
    @Autowired
    private ITourRole iTourRole;
    @Autowired
    private IUser iUser;

    //查询该角色的所加入的所有队伍信息
    @Override
    public JsonResult getAllTeamInforByUserId(String userId) {
        JsonResult jsonResult = new JsonResult();
        try {
            List<TbTourTeam> allTeamInforByUserId = iTeam.getAllTeamInforByUserId(userId);
            if(allTeamInforByUserId.size()>0){
                jsonResult.setResult(allTeamInforByUserId);
                return jsonResult;
            }else{
                jsonResult.setMsg("你还没加入任何队伍");
                jsonResult.setErrorCode("400");
            }
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }

    //查找该队伍的所有用户
    @Override
    public JsonResult getAllUserInforByTeamId(String teamId) {
        JsonResult jsonResult = new JsonResult();
        try {
            List<TbUser> allUserInforByTeamId = iUser.getAllUserInforByTeamId(teamId);
            if (allUserInforByTeamId.size() > 0) {
                jsonResult.setResult(allUserInforByTeamId);
            } else {
                jsonResult.setMsg("队伍不存在");
                jsonResult.setErrorCode("400");
            }
        } catch (Exception e) {
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }

    //创建驴友队
    @Transactional
    @Override
    public JsonResult newTeam(TbTourTeam tourTeam, String userId) {
        String uuid = getUUID();
        JsonResult jsonResult = new JsonResult();
        try {
            int i = iTeam.insertTourTeam(uuid, uuid, userId, "log", "localtion", tourTeam.getMainProject(), tourTeam.getIntro(), tourTeam.getManifesto(), tourTeam.getPrincipalName(), tourTeam.getPrincipalTelphone(), uuid, uuid, getDate(), getDate(), tourTeam.getTeamName());
            int j = iUserTeam.insertTeamUserMap(userId, uuid, getDate(), getDate());
            /*int k = iTeamRole.insertTeamRoleMap(uuid,"1",teamRoleMap.getUpdateTime(),teamRoleMap.getCreateTime());*/
            int l = iUserRole.insertUserRoleMap(userId, "1", getDate(), getDate(), uuid);
            if (i != 0 && j != 0 && l != 0) {
                return jsonResult;
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                jsonResult.setErrorCode("400");
                jsonResult.setMsg("插入失败");
                return jsonResult;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }

    //根据ID查询驴友队
    @Override
    public JsonResult selectTourTeam(String teamId) {
        JsonResult jsonResult = new JsonResult();
        TbTourTeam tbTourTeam = iTeam.selectTeamById(teamId);
        jsonResult.setResult(tbTourTeam);
        return jsonResult;
    }

    //加入驴友队,
    @Transactional
    @Override
    public JsonResult putTourTeam(String teamId, String userId) {
        JsonResult jsonResult = new JsonResult();
        try {
            List<TbUserTeamMap> tbUserTeamMapByUserIdAndTeamId = iUserTeam.findTbUserTeamMapByUserIdAndTeamId(userId, teamId);
            if (tbUserTeamMapByUserIdAndTeamId.size() == 0) {
                int j = iUserTeam.insertTeamUserMap(userId, teamId, getDate() + "", getDate() + "");
                int i = iUserRole.insertUserRoleMap(userId, "4", getDate() + "", getDate() + "", teamId);
                /* int k = iTeamRole.insertTeamRoleMap(teamId,"4",getDate()+"",getDate()+"");*/
                if (i != 0 && j != 0) {
                    return jsonResult;
                } else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    jsonResult.setErrorCode("400");
                    jsonResult.setMsg("加入失败");
                    return jsonResult;
                }
            } else {
                jsonResult.setMsg("您已加入过该驴友队");
                return jsonResult;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }

        return jsonResult;
    }

    //退出驴友队
    @Transactional
    @Override
    public JsonResult deltourTeam(String userId, String teamId) {
        JsonResult jsonResult = new JsonResult();
        try {
            int i = iUserTeam.delUserRoleMapByUserId(userId);
            int j = iUserRole.delUserRoleMapByUserId(userId, teamId);
            if (i != 0 && j != 0) {
                return jsonResult;
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                jsonResult.setErrorCode("400");
                jsonResult.setMsg("退出驴友队失败");
                return jsonResult;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }


    //任命副队长
    @Transactional
    @Override
    public JsonResult upUserRoleByUserId(String userId, String teamId) {
        JsonResult jsonResult = new JsonResult();
        //传入userID，和角色id(2 是副队长)   2个字段都匹配上才可以
        List<TbTourRole> list = iTourRole.findTbTourRoleByRoleName("副队长");//得到副队长的角色id
        String captainRoleId = list.get(0).getId();
        List<TbUserRoleMap> userRoleByTeamId = iUserRole.getUserRoleByTeamId(teamId, captainRoleId);
        try {
            if (userRoleByTeamId.size() == 1) {
                String user = userRoleByTeamId.get(0).getUserId();//
                String roleId = iTourRole.findTbTourRoleByRoleName("队员").get(0).getId();//得到队员的角色id
                int i = iUserRole.upUserRoleMapByUserId(roleId, user, teamId);//原副队长变成小兵
                int k = iUserRole.upUserRoleMapByUserId(captainRoleId, userId, teamId);//提升为副队长
                if (i == 1 && k == 1) {
                    return jsonResult;
                } else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    jsonResult.setErrorCode("400");
                    jsonResult.setMsg("提升副队长失败");
                    return jsonResult;
                }
            }
            if (userRoleByTeamId.size() == 0) {//直接提升为队长
                int k = iUserRole.upUserRoleMapByUserId(captainRoleId, userId, teamId);//提升为副队长
                if (k == 1) {
                    return jsonResult;
                } else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    jsonResult.setErrorCode("400");
                    jsonResult.setMsg("提升副队长失败");
                    return jsonResult;
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }

    //取消副队长、管理员
    @Transactional
    @Override
    public JsonResult delUserRoleByUserId(String userId, String teamId) {
        JsonResult jsonResult = new JsonResult();
        List<TbTourRole> list = iTourRole.findTbTourRoleByRoleName("队员");//得到要取消职位的角色id
        String roleId = list.get(0).getId();
        try {
            int i = iUserRole.upUserRoleMapByUserId(roleId, userId, teamId);//职务变成队员
            if (i == 1) {
                return jsonResult;
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                jsonResult.setErrorCode("400");
                jsonResult.setMsg("取消失败");
                return jsonResult;
            }

        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return null;
    }

    //只有大队长和副队长能任命管理，管理只能有8位
    @Transactional
    @Override
    public JsonResult appointLeader(String userId, String teamId) {
        JsonResult jsonResult = new JsonResult();
        List<TbTourRole> list = iTourRole.findTbTourRoleByRoleName("管理员");
        String roleId = list.get(0).getId();//查管理的角色Id
        List<TbUserRoleMap> allGm = iUserRole.getUserRoleByTeamId(teamId, roleId);
        try {
            if (allGm.size() < 8) {
                int i = iUserRole.upUserRoleMapByUserId(roleId, userId, teamId);
                if (i == 1) {
                    return jsonResult;
                } else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    jsonResult.setErrorCode("400");
                    jsonResult.setMsg("提升管理员失败");
                    return jsonResult;
                }
            }
            if (allGm.size() == 8) {
                jsonResult.setMsg("管理员名额已满");
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return null;
    }
    //转让驴友队
    @Transactional
    @Override
    public JsonResult assignmentTourPal( String teamId,String userId) {
        JsonResult jsonResult = new JsonResult();
        try {
        //传入userID，和角色id(1 是队长)   2个字段都匹配上才可以
        List<TbTourRole> list = iTourRole.findTbTourRoleByRoleName("队长");//得到队长的角色id
        String captainRoleId = list.get(0).getId();
        List<TbUserRoleMap> userRoleByTeamId = iUserRole.getUserRoleByTeamId(teamId, captainRoleId);
            if (userRoleByTeamId.size() == 1) {//判断队伍是否存在
                String user = userRoleByTeamId.get(0).getUserId();// 队长id
                String roleId = iTourRole.findTbTourRoleByRoleName("队员").get(0).getId();//得到队员的角色id
                int i = iUserRole.upUserRoleMapByUserId(roleId, user, teamId);//原队长变成队员
                int k = iUserRole.upUserRoleMapByUserId(captainRoleId, userId, teamId);//提升为队长
                if (i == 1 && k == 1) {
                    return jsonResult;
                } else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    jsonResult.setErrorCode("400");
                    jsonResult.setMsg("提升队长失败");
                }
            }
            if (userRoleByTeamId.size() == 0) {
                jsonResult.setErrorCode("400");
                jsonResult.setMsg("队伍不存在");
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return  jsonResult;
    }
}
